﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Freeform;

namespace Space_in_Shaders
{
    public class EnemyShip : SpaceShip
    {

        public Single Thrust { get; set; }
        public Single TurnTorque { get; set; }
        public Single FireAngThreshold { get; set; }
        public Single FireDispThreshold { get; set; }
        public Boolean LeftThrust { get; set; }
        public Boolean UpThrust { get; set; }
        public Boolean DownThrust { get; set; }
        public Boolean RightThrust { get; set; }
        public Int32 TurnState { get; set; }

        public EnemyShip(SpaceWorld w)
            :base (w)
        {
            Game.LoadAnimation(this, Game.Random.Int32(0, 3) + GetType().Name);
            Actor.Depth = 0.2f;
            SpatialProperties.Size = new Vector2(9, 13);
            NMot.TorqueResistance = 0.995f;
            PuffSpacing = 200;
            NextPuff = PuffSpacing;
            Thrust = 50f;
            TurnTorque = 1f;
            LaserSpacing = 350;
            LaserSpeed = 180f;
            LaserOffset = 16;
            NextLaser = LaserSpacing;
            FireAngThreshold = 0.5f;
            FireDispThreshold = 18000f;
            CollisionProperties.CollisionID = RadialCollision.CID_AISHIP;
            Radial.Radius = 7;
            CollisionProperties.SetCollisionMask(RadialCollision.CID_LASER, true);
            CollisionProperties.SetCollisionMask(RadialCollision.CID_MISSILE, true);
            CollisionProperties.SetCollisionMask(RadialCollision.CID_AISHIP, true);
            CollisionProperties.SetCollisionMask(RadialCollision.CID_PLAYERSHIP, true);
            Health = 6;
            ExplosionIntensity = 6;
            Points = 1;
        }

        public override void Update(GameTime time)
        {

            base.Update(time);

            // Set the direction of ship facing the mouse pointer.
            Vector2 Disp = this.SpaceWorld.Player.SpatialProperties.Position - SpatialProperties.Position;
            Single ReqAng = (Single)Math.Atan2(Disp.X, -Disp.Y);
            Single Diff = ReqAng - SpatialProperties.Direction;
            if (Diff > Math.PI)
                Diff -= MathHelper.TwoPi;
            if (Diff < -Math.PI)
                Diff += MathHelper.TwoPi;

            TurnState = Diff > 0 ? 1 : -1;

            if (Math.Abs(Diff) < FireAngThreshold && Disp.LengthSquared() < FireDispThreshold)
                IsFiringLaser = true;
            else
                IsFiringLaser = false;
            
            // Switch between Firing and Idle animations.
            if (IsFiringLaser && ImageActor.Animations.Current.AnimationName != "Firing")
                ImageActor.Animations.PlayAnimation("Firing");
            else if (!IsFiringLaser && ImageActor.Animations.Current.AnimationName != "Normal")
                ImageActor.Animations.PlayAnimation("Normal");

            NMot.ApplyTorque(TurnTorque * TurnState);

            IsThrusting = true;

            if (IsThrusting)
                NMot.ApplyForce(Util.Vec2InDir(SpatialProperties.Direction) * Thrust);
        }
    }
}
